/***引入axios***/
import axios from 'axios';
import address from "@/assets/js/area";
import commonData from './commonData';
import router from '@/router'
/***mathjs***/
import * as mathjs from 'mathjs';

export default {

  /***get请求***/
  getAjax(url, dataObj) {
    return new Promise((resolve, reject) => {
      axios({
        method: 'get',
        url: "/admin-web" + url,
        params: dataObj,
      }).then(res => {
        resolve(res);
      })
    })
  },
  /***post请求***/
  postAjax(url, data) {
    return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url: '/admin-web' + url,
        data: data,
      }).then(res => {
        resolve(res);
      })
    })

  },
  /***post请求***/
  postCodeAjax(data) {
    return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url: 'http://www.cc-java.com:20001/randImage',
        data: data,
      }).then(res => {
        resolve(res);
      })
    })

  },
  /***post请求***/
  postForm(url, data) {
    return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url: '/admin-web' + url,
        transformRequest: [function (data) {
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        data: data,
      }).then(res => {
        resolve(res);
      })
    })
  },
  deleteAjax(url, dataObj) {
    return new Promise((resolve, reject) => {
      axios({
        method: 'delete',
        url: "/admin-web" + url,
        params: dataObj,
      }).then(res => {
        resolve(res);
      })
    })
  },
  /***手机号验证***/
  isPoneAvailable(val) {
    var myreg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    if (myreg.test(val)) {
      return true;
    } else {
      return false;
    }
  },
  /**小数验证*/
  isNumber(val) {
    var myreg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;
    if (myreg.test(val)) {
      return true;
    } else {
      return false;
    }
  },
  //整数验证
  isInteger(val) {
    var myreg = /^[0-9]*$/;
    if (myreg.test(val)) {
      return true;
    } else {
      return false;
    }
  },
  /**字符串转换成数字
   * Number(v)会自动将字符串转换成数字
   * Number("")会自动将空字符串转换成0
   * Number(undefined)会自动将undefined转换成NaN
   * Number(null)会自动将null转换成0
   * Number(false)会自动将false转换成0
   * Number(true)会自动将true转换成1
   * Number("abc")会自动将字符串abc转换成NaN
   * Number("123")会自动将字符串123转换成123
   */
  strToNumber(v) {
    if (v) {
      return Number(v);
    }else if(v==undefined){
      return "";
    } else {
      return v;
    }
  },

  /*身份证号码*/
  isCardId(val) {
    var myreg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/;
    console.log(myreg.test(val))
    console.log(val)
    if (myreg.test(val)) {
      return true;
    } else {
      return false;
    }
  },
  /**根据身份证号查性别 */
  getSexByIdCard(idNumber) {
    let genderCode = idNumber.charAt(16);
    if (parseInt(genderCode) % 2 == 0) {
      return 2;
    } else {
      return 1;
    }
  },
  /**保存搜索条件 */
  saveSearchCriteria(path, query) {
    router.replace({
      path,
      query
    });
  },
  /***数组判断***/
  isArrayFilter(val) {
    if (Array.isArray(val)) {
      return val;
    } else {
      return [];
    }
  },
  /***户籍性质***/
  censusType(c) {
    let arr = commonData.censusOptions;
    let resultArr = arr.filter((item) => {
      return item.value == c
    });
    if (resultArr.length > 0) {
      return resultArr[0].label
    } else {
      return '-'
    }
  },
  /**获取省市 */
  getAddress() {
    let cityArr = [];
    let provinceArr = [];
    for (let i = 11; i < 83; i++) {
      let children = [];
      let children2 = [];
      for (const key in address.city_list) {
        let obj = {};
        if (key.toString().substring(0, 2) < i + 1) {
          if (key.toString().substring(0, 2) == i) {
            obj.label = address.city_list[key];
            obj.value = key;
          }
          if (obj.label) {
            children.push(obj);
          }
        }
      }
      for (const key in address.province_list) {
        let obj = {};
        if (key.toString().substring(0, 2) < i + 1) {
          if (key.toString().substring(0, 2) == i) {
            obj.label = address.province_list[key];
            obj.value = key;
          }
          if (obj.label) {
            children2.push(obj);
          }
        }
      }
      if (children.length > 0) {
        cityArr.push(children);
      }
      if (children2.length > 0) {
        provinceArr.push(children2);
      }
    }
    let cityChildren = [...new Set(cityArr)];
    let provinceChildren = [...new Set(provinceArr)];
    for (let j = 0; j < provinceChildren.length; j++) {
      provinceChildren[j][0].children = cityChildren[j];
    }
    let newArr = [];
    for (let i = 0; i < provinceChildren.length; i++) {
      let obj = provinceChildren[i][0];
      newArr.push(obj);
    }
    return newArr;
  },
  /*获取省市区*/
  getAllAddress() {
    let cityArr = [];
    let provinceArr = [];
    let countyArr = [];
    for (let i = 11; i < 83; i++) {
      let children = [];
      let children2 = [];
      let children3 = [];
      for (const city in address.city_list) {
        if (city.toString().substring(0, 2) < i + 1) {
          if (city.toString().substring(0, 2) == i) {
            let newArr = [];
            for (const key in address.county_list) {
              let obj = {};
              if (key.toString().substring(0, 2) == i && parseInt(key) >= parseInt(city) && parseInt(key) < this.accAdd(city, 100)) {
                obj.label = address.county_list[key];
                obj.value = address.county_list[key];
                obj.areaCode = key;
                newArr.push(obj);
              }
            }
            if (newArr.length > 0) {
              children3.push(newArr);
            }

          }
        }

      }


      for (const key in address.city_list) {
        let obj = {};
        if (key.toString().substring(0, 2) < i + 1) {
          if (key.toString().substring(0, 2) == i) {
            obj.label = address.city_list[key];
            obj.value = address.city_list[key];
            obj.areaCode = key;
          }
          if (obj.label) {
            children.push(obj);
          }
        }
      }
      for (const key in address.province_list) {
        let obj = {};
        if (key.toString().substring(0, 2) < i + 1) {
          if (key.toString().substring(0, 2) == i) {
            obj.label = address.province_list[key];
            obj.value = address.province_list[key];
            obj.areaCode = key;
          }
          if (obj.label) {
            children2.push(obj);
          }
        }
      }
      if (children3.length > 0) {
        countyArr.push(children3);
      }
      if (children.length > 0) {
        cityArr.push(children);
      }
      if (children2.length > 0) {
        provinceArr = [...new Set(provinceArr), ...new Set(children2)];
      }
    }

    let countyChildren = [...new Set(countyArr)];
    let cityChildren = [...new Set(cityArr)];
    let provinceChildren = [...new Set(provinceArr)];
    let arrValue = [];
    for (let c = 0; c < cityChildren.length; c++) {
      for (let q = 0; q < cityChildren[c].length; q++) {
        cityChildren[c][q].children = countyChildren[c][q];
      }
      provinceChildren[c].children = cityChildren[c];
      let obj = provinceChildren[c];
      arrValue.push(obj);
    }
    return arrValue;
  },
  /***时间转换***/
  timeSwitch(val, judge) {
    let date = new Date(val);
    let Month = date.getMonth() + 1;
    let day = date.getDate();
    let hours = date.getHours();
    let minutes = date.getMinutes();
    let second = date.getSeconds();
    if (judge) {
      return date.getFullYear() + '-' + this.isTwo(Month) + '-' + this.isTwo(day) + ' ' + this.isTwo(hours) + ':' + this.isTwo(minutes) + ':' + this.isTwo(second);
    } else {
      return date.getFullYear() + '-' + this.isTwo(Month) + '-' + this.isTwo(day)
    }
  },
  //只显示年月
  monthSwitch(val) {
    if (val) {
      let date = new Date(val);
      let Month = date.getMonth() + 1;
      return date.getFullYear() + '-' + this.isTwo(Month)
    }
  },
  //显示2012年12月22日
  isNormalDate(val) {
    let date = new Date(val);
    let Month = date.getMonth() + 1;
    let day = date.getDate();
    return date.getFullYear() + '年' + this.isTwo(Month) + '月' + this.isTwo(day) + '日';
  },
  isTwo(val) {
    if (val.toString().length > 1) {
      return val;
    } else {
      return '0' + val;
    }
  },
  //菜单
  getMenu(e) {
    if (e == 'user') {
      let userMenu = [
        // {
        //   icon: "icontongji-",
        //   name: "首页",
        //   url: "homePage",
        //   title: "首页"
        // },
        {
          icon: "iconrenyuanguanli",
          name: "个人信息",
          url: "personalInfo",
          title: "个人信息"
        },
        {
          icon: "iconzhangdanguanli",
          name: "订单管理",
          url: "billManagement",
          title: "订单管理"
        },
        // {
        //   icon: "iconfapiaoguanli",
        //   name: "发票管理",
        //   url: "invoiceManagement",
        //   title: "发票管理"
        // },
        {
          icon: "iconjine",
          name: "账户流水",
          url: "balanceManagement",
          title: "账户流水"
        },
        // {
        //   icon: "icon18gengduo",
        //   name: "更多功能",
        //   url: "moreFunctions",
        //   title: "五险一金计算器"
        // }
      ];
      return userMenu;
    } else if (e == 'admin') {
      let adminMenu = [
        // {
        //   icon: "icontongji-",
        //   name: "首页",
        //   url: "homePage",
        //   title: "首页"
        // },
        {
          icon: "iconshenhe1",
          name: "人员审核",
          url: "auditList",
          title: "人员审核"
        },
        {
          icon: "iconqiye",
          name: "企业审核",
          url: "auditCompany",
          title: "企业审核"
        },
        {
          icon: "iconyonghuguanli",
          name: "人员管理",
          url: "userManagement",
          title: "人员管理"
        },

        {
          icon: "iconqiye2",
          name: "企业管理",
          url: "businessManagement",
          title: "企业管理"
        },
        // {
        //   icon: "icongerensuode",
        //   name: "个体管理",
        //   url: "personageManagement",
        //   title: "个体管理"
        // },
        {
          icon: "iconxiangmu1",
          name: "缴纳项目",
          url: "PayTheProject",
          title: "缴纳项目"
        },
        {
          icon: "iconquyuguanli",
          name: "区域缴纳",
          url: "payManagement",
          title: "区域缴纳"
        },
        {
          icon: "iconpingtaiguanli",
          name: "缴纳平台",
          url: "platformManagement",
          title: "缴纳平台"
        },
        {
          icon: "icontubiaozhizuomoban-24",
          name: "订单类型",
          url: "orderTypeManagement",
          title: "订单类型"
        },
        {
          icon: "iconzhangdanguanli",
          name: "订单管理",
          url: "billManagement",
          title: "订单管理"
        },
        // {
        //   icon: "iconfapiaoguanli",
        //   name: "发票管理",
        //   url: "invoiceManagement",
        //   title: "发票管理"
        // },
        // {
        //   icon: "iconjine",
        //   name: "账户流水",
        //   url: "balanceManagement",
        //   title: "账户流水"
        // },
        {
          icon: "icontubiaozhizuomoban-70",
          name: "合同管理",
          url: "contractManagement",
          title: "合同管理"
        },
        // {
        //   icon: "icon18gengduo",
        //   name: "更多功能",
        //   url: "moreFunctions",
        //   title: "五险一金计算器"
        // }
      ];
      return adminMenu;
    } else if (e == 'company') {
      let companyMenu = [
        // {
        //   icon: "icontongji-",
        //   name: "首页",
        //   url: "homePage",
        //   title: "首页"
        // },
        {
          icon: "iconguanliyuan1",
          name: "员工名录",
          url: "employeeList",
          title: "员工名录"
        },
        {
          icon: "iconzhangdanguanli",
          name: "订单管理",
          url: "billManagement",
          title: "订单管理"
        },
        // {
        //   icon: "iconfapiaoguanli",
        //   name: "发票管理",
        //   url: "invoiceManagement",
        //   title: "发票管理"
        // },
        // {
        //   icon: "iconjine",
        //   name: "账户流水",
        //   url: "balanceManagement",
        //   title: "账户流水"
        // },
        // {
        //   icon: "icon18gengduo",
        //   name: "更多功能",
        //   url: "moreFunctions",
        //   title: "五险一金计算器"
        // }
      ];
      return companyMenu;
    } else if (e == 'check') {
      let menu = [
        {
          icon: "iconshenhe1",
          name: "人员审核",
          url: "auditList",
          title: "人员审核"
        },
        {
          icon: "iconqiye",
          name: "企业审核",
          url: "auditCompany",
          title: "企业审核"
        },
        {
          icon: "iconyonghuguanli",
          name: "人员管理",
          url: "userManagement",
          title: "人员管理"
        },
        // {
        //   icon: "iconqiye2",
        //   name: "企业管理",
        //   url: "businessManagement",
        //   title: "企业管理"
        // },
        // {
        //   icon: "iconzhangdanguanli",
        //   name: "订单管理",
        //   url: "billManagement",
        //   title: "订单管理"
        // },
        // {
        //   icon: "iconfapiaoguanli",
        //   name: "发票管理",
        //   url: "invoiceManagement",
        //   title: "发票管理"
        // },
      ];
      return menu;

    } else if (e == 'operator') {
      let menu = [
        {
          icon: "iconqiye",
          name: "企业审核",
          url: "auditCompany",
          title: "企业审核"
        },
        {
          icon: "iconqiye2",
          name: "企业管理",
          url: "businessManagement",
          title: "企业管理"
        },
        // {
        //   icon: "icongerensuode",
        //   name: "个体管理",
        //   url: "personageManagement",
        //   title: "个体管理"
        // },
        // {
        //   icon: "icon18gengduo",
        //   name: "更多功能",
        //   url: "moreFunctions",
        //   title: "五险一金计算器"
        // }
      ];
      return menu;
    } else if (e == "finance") {
      let menu = [
        {
          icon: "iconqiye2",
          name: "企业管理",
          url: "businessManagement",
          title: "企业管理"
        },
        {
          icon: "iconzhangdanguanli",
          name: "订单管理",
          url: "billManagement",
          title: "订单管理"
        },
        {
          icon: "iconshoukuanmingxi1",
          name: "收款明细",
          url: "collectionSchedule",
          title: "收款明细"
        },
      ];
      return menu;
    }
  },
  /*
  数字格式化
  整数.00
  0不处理
  小数，小数点后不足2位，保留2位小数【无需四舍五入】
  */
  keepTwo(e) {
    let rounded = e.toFixed(2);
    let padded = rounded.padEnd(rounded.indexOf('.') + 3, '0');
    return padded;
    // let numArr = e.toString().split(".");
    // if (numArr.length > 1) {
    //   if (numArr[1].length >= 2) {
    //     //小数点后两位,无需四舍五入
    //     return e.toString();
    //   } else {
    //     let diff = 2 - (numArr[1].length || 0);
    //     return numArr[0] + "." + numArr[1] + diff * "0";
    //   }
    // } else {
    //   return e.toString();
    // }
  },
  //乘法
  //parseFloat解决科学计数法
  accMul(arg1 = 0, arg2 = 0) {
    let value = mathjs.format(mathjs.multiply(mathjs.bignumber(arg1), mathjs.bignumber(arg2)));
    return parseFloat(value);
  },
  //除法
  accDiv(arg1 = 0, arg2 = 0) {
    let value = mathjs.format(mathjs.divide(mathjs.bignumber(arg1), mathjs.bignumber(arg2)));
    return parseFloat(value);
  },
  //加法
  accAdd(arg1 = 0, arg2 = 0) {
    let value = mathjs.format(mathjs.add(mathjs.bignumber(arg1), mathjs.bignumber(arg2)));
    return parseFloat(value);

  },
  //减法
  accSub(arg1 = 0, arg2 = 0) {
    let value = mathjs.format(mathjs.subtract(mathjs.bignumber(arg1), mathjs.bignumber(arg2)));
    return parseFloat(value);
  }
}
